package com.wrench.elevator.tida.controller;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.DataCollection;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.service.BuildingService;
import com.wrench.elevator.tida.service.DataCollectionService;
import com.wrench.elevator.tida.util.WrenchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/dataCollection")
public class DataCollectionController {
	private static final Logger logger = LoggerFactory
			.getLogger(BuildingManageController.class);

	@Resource
	private DataCollectionService dataCollectionService;

	
	
	@RequestMapping("/getYesterdayRecharge")
	public void getYesterdayRecharge(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getYesterdayRecharge(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");
		WrenchException error = null;
		Page page = null;
		try {
			page = dataCollectionService.getYesterdayRecharge(building_id,
					current_page, limit);
		} catch (WrenchException e) {
			// TODO Auto-generated catch block
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("page", page);
		} else {
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out getYesterdayRecharge(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}
	
	
	@RequestMapping("/getYesterdayChengti")
	public void getYesterdayChengti(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getYesterdayChengti(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");
		WrenchException error = null;
		Page page = null;
		try {
			page = dataCollectionService.getYesterdayChengti(building_id,
					current_page, limit);
		} catch (WrenchException e) {
			// TODO Auto-generated catch block
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("page", page);
		} else {
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out getYesterdayChengti(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}
	
	
	@RequestMapping("/getYesterdayNewReg")
	public void getYesterdayNewReg(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in yesterdayNewReg(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");
		WrenchException error = null;
		Page page = null;
		try {
			page = dataCollectionService.getYesterdayNewReg(building_id,
					current_page, limit);
		} catch (WrenchException e) {
			// TODO Auto-generated catch block
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("page", page);
		} else {
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out yesterdayNewReg(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	@RequestMapping("/getSumDetails")
	public void getSumDetails(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getSumDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		Map<String, String> data = null;
		WrenchException error = null;
		try {
			data = dataCollectionService.getSumDetail(building_id);
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("data", data);
		} else {
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out getSumDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}

	@RequestMapping("/getRecentChengtiDetails")
	public void getRecentChengtiDetails(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getRecentChengtiDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		String current_page = WrenchUtil.getPara(request, "current_page");
		String limit = WrenchUtil.getPara(request, "limit");
		WrenchException error = null;
		Page page = null;
		try {
			page = dataCollectionService.getRecentChengtiDetails(building_id,
					current_page, limit);
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("page", page);
		} else {
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;

		logger.debug("==>**********exit out getRecentChengtiDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	@RequestMapping("/get7DaysChengtiDetails")
	public void get7DaysChengtiDetails(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in get7DaysChengtiDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

		String building_id = WrenchUtil.getPara(request, "wuye_id");
		WrenchException error = null;
		List<String> list = null;
		try {
			list = dataCollectionService.get7DaysChengtiDetails(building_id);
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "get7DaysChengtiDetails success");
			Map m1 = new HashMap();
			List<String> strs = new ArrayList<String>();
			strs.add("近期乘梯次数");
			m1.put("data", strs);
			map.put("legend", m1);
			List<Map> maps = new ArrayList<>();
			Map m2 = new HashMap();
			m2.put("name", "近期乘梯次数");
			m2.put("type", "value");
			m2.put("data", list);
			maps.add(m2);
			map.put("series", maps);

			List<Map> maps2 = new ArrayList<>();
			Map m3 = new HashMap();
			m3.put("type", "category");

			List<String> data = new ArrayList<String>();
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
			Date date = new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			data.add(sdf.format(calendar.getTime()));

			Collections.reverse(data);

			m3.put("data", data);

			maps2.add(m3);
			map.put("xAxis", maps2);

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out get7DaysChengtiDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	@RequestMapping("/getDataCollection")
	public void queryDanyuanByBuildingName(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getDataCollection(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
		String building_id = WrenchUtil.getPara(request, "wuye_id");
		WrenchException error = null;
		DataCollection dc = null;
		try {
			dc = dataCollectionService.getDataCollection(building_id);
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "getDataCollection success");
			map.put("dataCollection", dc);

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out getDataCollection(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

	}

	@RequestMapping("/getRegDetails")
	public void getRegDetails(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		logger.debug("==>**********enter in getRegDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

		WrenchException error = null;
		List<Map<String, String>> data = null;

		try {
			data = dataCollectionService.getRegDetails();
		} catch (WrenchException e) {
			error = e;
		}
		if (WrenchUtil.hasNull(error)) {
			map.clear();
			map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
			map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
			map.put("message", "getDataCollection success");
			map.put("data", data);

		} else {
			map.clear();
			map.put(Result.RESULT_CODE, error.getResultCode());
			map.put(Result.RESULT_MESSAGE, error.getResultMessage());
		}
		logger.debug("==> response = {}", JSON.toJSON(map));
		WrenchUtil.responseMap(response, map);
		map = null;
		logger.debug("==>**********exit out getRegDetails(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
	}
}
